package cn.yunhe.controller;

import cn.yunhe.entity.UserInfo;
import cn.yunhe.service.RoleService;
import cn.yunhe.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size, String userName) {
        PageHelper.startPage(page, size);
        List<UserInfo> usersList = null;
        if (userName != null && !"".equals(userName)) {
            usersList = userService.findUserByLike(userName);
        } else {
            usersList = userService.findAll();
        }

        PageInfo<UserInfo> pageInfo = new PageInfo<>(usersList);
        model.addAttribute("pageInfo", pageInfo);
        return "user-list";
    }

    @RequestMapping("/save")
    public String save(UserInfo userInfo) {

        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        userService.insertUser(userInfo);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(@RequestParam("id") Integer userId) {
        userService.deleteUser(userId);
        return "redirect:findAll";
    }

    @RequestMapping("findUserByIdAndAllRole")
    public String findUserByIdAndAllRole(Model Model, Integer id) {
        UserInfo userInfo = userService.findUserByIdAndAllRole(id);
        Model.addAttribute("user", userInfo);
        Model.addAttribute("roleList", userInfo.getRoles());
        return "user-role-add";
    }

    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(Integer[] ids, Integer userId) {
        roleService.addRoleToUser(ids, userId);
        return "redirect:findAll";
    }

    @RequestMapping("findById")
    public String findById(Model model, Integer id) {
        UserInfo userInfo = userService.findById(id);
        model.addAttribute("user", userInfo);
        return "user-show";
    }

    @RequestMapping("getUsername")
    @ResponseBody
    public String getUserName() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        User user = (User) authentication.getPrincipal();
        return user.getUsername();
    }

}
